package com.mspbots.rule.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mspbots.api.client.CoreClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

@Slf4j
@Configuration
@EnableScheduling
@EnableAsync
public class RuleConfiguration {

    public static final String REDIS_KEY_PREFIX_SYSTEM = "MSPBOTS:CONFIG:SYSTEM:";
    public static final String REDIS_KEY_PREFIX_TENANT = "MSPBOTS:CONFIG:TENANT:";
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private CoreClient coreClient;

    /*@Bean
    public RejectedExecutionHandler rejectedExecutionHandler() {
        return new RejectedExecutionHandler() {
            @Override
            public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                if (!executor.isShutdown()){
                    try {
                        log.info("thread put into queue... {} ",executor.getTaskCount());
                        executor.getQueue().put(r);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
    }*/

    /*@Bean
    public CommandLineRunner start() {
        return args -> {
            this.coreClient.getFlux("/system-configuration/list")
                    .subscribe(dto ->
                            stringRedisTemplate.opsForValue()
                                    .set(REDIS_KEY_PREFIX_SYSTEM + dto.get("keyword").asText(), dto.get("keywordValue").asText())
                    );

            this.coreClient.getFlux("/tenant")
                    .doOnNext(dto -> {
                        this.coreClient.getFlux("/tenant/configuration/tenantId/" + dto.get("id"))
                                .subscribe(tcd ->
                                        stringRedisTemplate.opsForValue()
                                                .set(REDIS_KEY_PREFIX_TENANT + tcd.get("tenantId")
                                                        + ":" + tcd.get("keyword").asText(), tcd.get("keywordValue").asText()));

                        this.coreClient.getFlux("/tenant/rule/list", Map.of("tenantId", dto.get("id")))
                                .doOnNext(rule -> {
                                    log.info("rule_cache_{}_{}_{}", rule.get("tenantId"), rule.get("ruleId"), rule.get("sending").toString());
                                    stringRedisTemplate.opsForValue().set(REDIS_KEY_PREFIX_TENANT + rule.get("tenantId") + ":"
                                            + rule.get("ruleId") + "_sending", rule.get("sending").toString());
                                    stringRedisTemplate.opsForValue().set(REDIS_KEY_PREFIX_TENANT + rule.get("tenantId") + ":"
                                            + rule.get("ruleId") + "_enable", rule.get("enable").asText());
                                })
                                .subscribe();
                        stringRedisTemplate.opsForValue().set(REDIS_KEY_PREFIX_TENANT + dto.get("id") + ":"
                                + 900 + "_sending", "true");

                    })
                    .map(item -> objectMapper.convertValue(item, TenantDTO.class))
                    .collectList()
                    .subscribe(dtos -> {
                        this.redisTemplate.delete(REDIS_KEY_PREFIX_TENANT + "tenants");
                        this.redisTemplate.opsForList().leftPushAll(REDIS_KEY_PREFIX_TENANT + "tenants", Arrays.asList(dtos.toArray()));
                    });

        };

    }*/
}

